package com.example.myapplication;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
import java.net.SocketException;
import java.util.Iterator;

public class ServerThread implements Runnable {
    Socket s=null;
    BufferedReader br=null;
    public ServerThread(Socket s)throws IOException{
        this.s=s;
        br= new BufferedReader(new InputStreamReader(s.getInputStream(),"utf-8"));
    }

    @Override
    public void run() {
        try{
            String content=null;
            while ((content=readFromClient())!=null){
                for (Iterator<Socket>it=SimpleServer.socketList.iterator();((Iterator) it).hasNext();){
                    Socket s=it.next();
                    try {
                        OutputStream os=s.getOutputStream();
                        os.write((content+"\n").getBytes("utf-8"));



                    }catch (SocketException e){
                        e.printStackTrace();
                        it.remove();
                        System.out.println(SimpleServer.socketList);
                    }
                }
            }
        }catch (IOException e){
            e.printStackTrace();
        }
    }

    private String readFromClient(){

        try {
            return br.readLine();
        }catch (IOException e){
            e.printStackTrace();
            SimpleServer.socketList.remove(s);
        }
        return null;
    }
}
